package cn.com.git.example.config;

import cn.com.git.example.common.ResultConstant;
import cn.com.git.example.common.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 * @author zhangzhenfu
 * @Description
 * @date 2021/4/7 0007 21:15
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @Value("${spring.profiles.active}")
    private String profile;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultMsg<String> handleException(HttpServletRequest request, Exception e) {
        String requestURI = request.getRequestURI();
        e.printStackTrace();
        log.error("请求 {} 异常 {}",requestURI,e.getMessage());
        String message;
        if ("pro".equals(profile)) {
            message = "服务器内部异常,请稍后重试！";
        } else {
            message = e.getMessage();
        }
        return new ResultMsg<String>(ResultConstant.FAILED,message);
    }

}
